        $(document).ready(function () {
            // 学科变化时加载章节
            $('#subject').change(function () {
                var subjectId = $(this).val();
                console.log('学科ID变化:', subjectId);
                if (subjectId) {
                    var url = "{% url 'core:ajax_chapters' %}";
                    console.log('生成的章节URL:', url);
                    console.log('发送的subject_id:', subjectId);
                    $.getJSON(url, { subject_id: subjectId }, function (data) {
                        console.log('章节数据返回:', data);
                        var chapterSelect = $('#chapter');
                        chapterSelect.empty().append('<option value="">选择章节</option>');
                        $.each(data, function (index, chapter) {
                            chapterSelect.append('<option value="' + chapter.id + '">' + chapter.name + '</option>');
                        });
                        // 如果是编辑页面且有章节ID，选中对应章节并触发知识点加载
                        {% if mistake and mistake.chapter.id %}
                        chapterSelect.val({{ mistake.chapter.id }});
                    chapterSelect.trigger('change');
                    {% else %}
                    $('#knowledge_point').empty().append('<option value="">选择知识点</option>');
                    {% endif %}
                }).fail(function (jqXHR, textStatus, errorThrown) {
                    console.error('章节加载失败详情:', {
                        status: jqXHR.status,
                        statusText: jqXHR.statusText,
                        responseText: jqXHR.responseText,
                        textStatus: textStatus,
                        errorThrown: errorThrown
                    });
                });
        } else {
            $('#chapter').empty().append('<option value="">选择章节</option>');
            $('#knowledge_point').empty().append('<option value="">选择知识点</option>');
        }
                });

        // 章节变化时加载知识点
        $('#chapter').change(function () {
            var chapterId = $(this).val();
            if (chapterId) {
                var knowledgeUrl = "{% url 'core:ajax_knowledge_points' %}";
                console.log('生成的知识点URL:', knowledgeUrl);
                console.log('发送的chapter_id:', chapterId);
                $.getJSON(knowledgeUrl, { chapter_id: chapterId }, function (data) {
                    var knowledgePointSelect = $('#knowledge_point');
                    knowledgePointSelect.empty().append('<option value="">选择知识点</option>');
                    $.each(data, function (index, point) {
                        knowledgePointSelect.append('<option value="' + point.id + '">' + point.name + '</option>');
                    });
                    // 如果是编辑页面且有知识点ID，选中对应知识点
                    {% if mistake and mistake.knowledge_point.id %}
                    knowledgePointSelect.val({{ mistake.knowledge_point.id }});
                {% endif %}
            }).fail(function (jqXHR, textStatus, errorThrown) {
                console.error('知识点加载失败详情:', {
                    status: jqXHR.status,
                    statusText: jqXHR.statusText,
                    responseText: jqXHR.responseText,
                    textStatus: textStatus,
                    errorThrown: errorThrown
                });
            });
                } else {
            $('#knowledge_point').empty().append('<option value="">选择知识点</option>');
        }
                });

        // 页面加载时如果已有学科选择，触发章节加载
        if ($('#subject').val()) {
            $('#subject').trigger('change');
        }
            });

        // 章节变化时加载知识点
        $('#chapter').change(function () {
            var chapterId = $(this).val();
            if (chapterId) {
                var knowledgeUrl = "{% url 'core:ajax_knowledge_points' %}";
                console.log('生成的知识点URL:', knowledgeUrl);
                console.log('发送的chapter_id:', chapterId);
                $.getJSON(knowledgeUrl, { chapter_id: chapterId }, function (data) {
                    var knowledgePointSelect = $('#knowledge_point');
                    knowledgePointSelect.empty().append('<option value="">选择知识点</option>');
                    $.each(data, function (index, point) {
                        knowledgePointSelect.append('<option value="' + point.id + '">' + point.name + '</option>');
                    });
                });
            } else {
                $('#knowledge_point').empty().append('<option value="">选择知识点</option>');
            }
        });